const _DB = require('../../utils/db.js');
const app = getApp();

Page({

  data: {
    disabled: false,
    btnstate: "primary",
    studentID: "",
  },

  onLoad: function (options) {

  },
  //修改密码
  // alterpassword:function(){
  //   wx.navigateTo({
  //     url: '/pages/teacherlogin/password/password',
  //   })
  // },

  login:function(e){
    var teacherID = e.detail.value.name;
    var password = e.detail.value.password;

    if(teacherID.length == 4&&password != ''){
      wx.showToast({
        title: '正在登录',
        icon: 'loading',
        mask:true,
      })
      _DB.getTeacherByid(teacherID, function (res) {
        if(res.length == 0){
          wx.showToast({
            title: '您的工号还未注册，请先注册！',
            icon: 'none'
          })
        }else{
          if(password != res[0].password){
            wx.showToast({
              title: '密码错误！',
              icon: 'none'
            })
          }else{
            //成功登录情况
            wx.clearStorageSync();
            wx.setStorageSync('teacher',{'teacherID':res[0].teacherID,'name':res[0].name,'login':true});
            // wx.reLaunch({
            //   url: './teacher/teacher?teacherID=' + res[0].teacherID + '&&name=' + res[0].name,
            // })

            // console.log(res)
            app.globalData.teacherID = res[0].teacherID,
            app.globalData.teacherName = res[0].name,  
            wx.reLaunch({
              url: './home/home'
            })
          }

        }
      })
    }else{
      wx.showToast({
        title: '请输入账号密码',
        icon:'none'
      })
    }

  },

  //注册
  // loginteacher:function(){
  //   wx.navigateTo({
  //     url: './teacherreg/teacherreg',
  //   })
  // }
 
})